<template>
  <div class="about">
    <h1>{{ id ? "编辑" : "新建" }}管理员</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="用户名">
        <el-input v-model="model.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="model.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { ref, onBeforeMount } from "vue";
import { useRouter } from "vue-router";
import { postNewCategory, getDetail, putDetail, getList } from "@/api";
import { ElMessage } from "element-plus";
export default {
  props: {
    id: {},
  },
  setup(props) {
    var model = ref({});
    const router = useRouter();
    async function save() {
      let res = props.id
        ? await putDetail('admin_user',props.id, model)
        : await postNewCategory('admin_user',model);
      console.log(res);
      router.push("/admin_user/list");
      ElMessage({
        message: "保存成功",
        type: "success",
      });
    }
    async function fetch() {
      const res = await getDetail('admin_user',props.id);
      model.value = res.data;
    }
    onBeforeMount(() => {
      // 组件初次在页面渲染之前（创建阶段）唯一一次
      props.id && fetch();
    });
    return { model, save };
  },
};
</script>

<style></style>
